package com.example.demo.timu.chanshu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.easyexcel.db.entity.City;
import com.example.demo.timu.tianyiyun.Tiku;
import com.example.demo.timu.tianyiyun.TikuRepository;
import com.example.demo.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author: zjq
 * @Date: create in 2023/9/20 9:17
 */
@Service
public class TikusServiceImpl implements TikusService {

    @Autowired
    private TikusRepository tikusRepository;
    @Autowired
    private TikuRepository tikuRepository;

    @Override
    public Object like(String obj) {
        String param = '%' + obj + '%';
        List<Tikus> list = tikusRepository.getTikus(param);
        return ResultUtil.success("data", list);
    }

    @Override
    @Transactional
    public Object change() {
        List<Map<String, String>> map = tikusRepository.getAllTikus();

        String str = JSON.toJSONString(map);
        List<Tiku> jsonList = JSONArray.parseArray(str, Tiku.class);

        tikuRepository.saveAll(jsonList);
        tikusRepository.truncateTable();
        return ResultUtil.success("data", map.size());
    }
}
